/**
 * Created with JetBrains WebStorm.
 * User: Администратор
 * Date: 8/27/13
 * Time: 8:49 PM
 * To change this template use File | Settings | File Templates.
 */
function ShowTack(){

    var arrObj;
    var latAverage, lngAverage, dist;
    var lat1,lng1, lat2,lng2;
    var R = 6372795;

    this.init = function(){
        arrObj = [];

    };
    this.close = function(){

    };

    this.open = function(arr){
        var scope = this;
        scope.init();
        for(var i = 0; i<arr.length-1;i++){
            arrObj[i]={
                lat: parseFloat(arr[i].split(',')[1]),
                lng: parseFloat(arr[i].split(',')[2])
            }
        };
        scope.searchParking(arrObj)
        //alert(arrObj[2].lat)
    };

    this.searchParking = function(arrObj){
        var scope = this;
        var obj1,obj2;
        var d14, dAvrg;
        for (var i = 0; i<arrObj.length-5; i++){

            latAverage =   (arrObj[i+1].lat + arrObj[i+2].lat + arrObj[i+3].lat + arrObj[i+4].lat + arrObj[i+5].lat)/5;
            lngAverage = (arrObj[i+1].lng + arrObj[i+2].lng + arrObj[i+3].lng + arrObj[i+4].lng + arrObj[i+5].lng)/5;
            obj1={
                lat:arrObj[i].lat,
                lng:arrObj[i].lng
            };
            obj2={
                lat:latAverage,
                lng:lngAverage
            };
            //alert(obj1.lat+' ' + obj2.lat+' '+obj1.lng+' ' + obj2.lng);
          // alert(scope.latlng2distance(obj1,obj2));
            d14 = scope.latlng2distance(obj1,obj2);
            var d23 = scope.latlng2distance({
                lat:arrObj[i+1].lat,
                lng:arrObj[i+1].lng
            },{
                lat:arrObj[i+2].lat,
                lng:arrObj[i+2].lng
            });
            var d34 =
                scope.latlng2distance({
                    lat:arrObj[i+2].lat,
                    lng:arrObj[i+2].lng
                },{
                    lat:arrObj[i+3].lat,
                    lng:arrObj[i+3].lng
                });
            var d45 =
                scope.latlng2distance({
                    lat:arrObj[i+3].lat,
                    lng:arrObj[i+3].lng
                },{
                    lat:arrObj[i+4].lat,
                    lng:arrObj[i+4].lng
                });
            var d56 =
                scope.latlng2distance({
                    lat:arrObj[i+4].lat,
                    lng:arrObj[i+4].lng
                },{
                    lat:arrObj[i+5].lat,
                    lng:arrObj[i+5].lng
                });


            dAvrg = (d23+d34+d45+d56)/4;

            if (3*dAvrg<d14){
                L.marker([obj1.lat, obj1.lng]).addTo(map);
            }else{
            //  L.marker([obj2.lat, obj2.lng]).addTo(map);
                //alert(obj2.lat+' ' +obj2.lng);
            }


        }
    };

    this.latlng2distance = function(obj1, obj2){
        lat1 = obj1.lat; lng1 = obj1.lng;
        lat2 = obj2.lat; lng2 = obj2.lng;

        lat1 *= Math.PI / 180;
        lat2 *= Math.PI / 180;
        lng1 *= Math.PI / 180;
        lng2 *= Math.PI / 180;

        //вычисление косинусов и синусов широт и разницы долгот
        var cl1 = Math.cos(lat1);
        var cl2 = Math.cos(lat2);
        var sl1 = Math.sin(lat1);
        var sl2 = Math.sin(lat2);
        var delta = lng2 - lng1;
        var cdelta = Math.cos(delta);
        var sdelta = Math.sin(delta);

        //вычисления длины большого круга
        var y = Math.sqrt(Math.pow(cl2 * sdelta, 2) + Math.pow(cl1 * sl2 - sl1 * cl2 * cdelta, 2));
        var x = sl1 * sl2 + cl1 * cl2 * cdelta;
        var ad = Math.atan2(y, x);
        var dist = ad * R; //расстояние между двумя координатами в метрах
        return  dist; //расстояние между двумя координатами в метрах

    }
}
var showTrack = new ShowTack();